import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:html/dom.dart' as dom;
import '../../service/service_method.dart';

class BlocDocs extends StatefulWidget {
  Map arguments;
  BlocDocs({Key key, this.arguments}) : super(key: key);

  @override
  _BlocDocsState createState() => _BlocDocsState(arguments);
}

class _BlocDocsState extends State<BlocDocs> {
  _BlocDocsState(this.arguments);
  Map arguments;
  String docs;
  @override
  void initState() {
    super.initState();
    getBlogDocs(arguments['id']).then((value) {
      this.docs = value['htmldocs'];
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        leading: IconButton(
            icon: Icon(
              Icons.keyboard_arrow_left,
              size: 30,
              color: Colors.black,
            ),
            onPressed: () {
              Navigator.of(context).pop();
            }),
        backgroundColor: Color.fromRGBO(242, 242, 242, 1),
        actions: <Widget>[
          FlatButton(
            color: Color.fromRGBO(242, 242, 242, 1),
            onPressed: () {
              print("用户点击了收藏按钮");
            },
            child: Text("收藏"),
          )
        ],
      ),
      body: SingleChildScrollView(
        child: Column(
          children: <Widget>[
            Html(
              data: """
            $docs
  """,
              //Optional parameters:
              padding: EdgeInsets.all(8.0),
              linkStyle: const TextStyle(
                color: Colors.redAccent,
                decorationColor: Colors.redAccent,
                decoration: TextDecoration.underline,
              ),
              onLinkTap: (url) {
                print("Opening $url...");
              },
              onImageTap: (src) {
                print(src);
              },
              //Must have useRichText set to false for this to work
              customRender: (node, children) {
                if (node is dom.Element) {
                  switch (node.localName) {
                    case "custom_tag":
                      return Column(children: children);
                  }
                }
                return null;
              },
              customTextAlign: (dom.Node node) {
                if (node is dom.Element) {
                  switch (node.localName) {
                    case "p":
                      return TextAlign.justify;
                  }
                }
                return null;
              },
              customTextStyle: (dom.Node node, TextStyle baseStyle) {
                if (node is dom.Element) {
                  switch (node.localName) {
                    case "p":
                      return baseStyle
                          .merge(TextStyle(height: 2, fontSize: 20));
                  }
                }
                return baseStyle;
              },
            ),
          ],
        ),
      ),
    );
  }
}
